﻿
using Android.App;
using Android.Content;
using Android.OS;
using Android.Media;
using Android.Media.Session;
using System;
using Android.Util;

namespace TestActionBar
{
	[Service]
	public class MediaPlayerService : Service
	{

		private MediaPlayer m_objMediaPlayer;
		private MediaSessionManager m_objMediaSessionManager;
		public static MediaSession m_objMediaSession;
		public static Context mContext;
		public static Android.Media.Session.MediaController m_objMediaController;
		string musicUrl = "http://ccmixter.org/content/bradstanfield/bradstanfield_-_People_Let_s_Stop_The_War.mp3";

		#region implemented abstract members of Service

		public override IBinder OnBind (Intent intent)
		{
			return null;
		}

		#endregion

		public void HandleIntent (Intent intent)
		{
			if (intent == null || intent.Action == null)
				return;

			string action = intent.Action;

			if (action.Equals (Constants.ACTION_PLAY)) {
				m_objMediaController.GetTransportControls ().Play ();
			} else if (action.Equals (Constants.ACTION_PAUSE)) {
				m_objMediaController.GetTransportControls ().Pause ();
			} else if (action.Equals (Constants.ACTION_FAST_FORWARD)) {
				m_objMediaController.GetTransportControls ().FastForward ();
			} else if (action.Equals (Constants.ACTION_REWIND)) {
				m_objMediaController.GetTransportControls ().Rewind ();
			} else if (action.Equals (Constants.ACTION_NEXT)) {
				m_objMediaController.GetTransportControls ().SkipToNext ();
			} else if (action.Equals (Constants.ACTION_STOP)) {
				m_objMediaController.GetTransportControls ().Stop ();
			} else if (action.Equals (Constants.ACTION_PREVIOUS)) {
				m_objMediaController.GetTransportControls ().SkipToPrevious ();
			} 
		}


		public override StartCommandResult OnStartCommand (Intent intent, StartCommandFlags flags, int startId)
		{
			if (m_objMediaSessionManager == null) {
				InitMediaSessions ();

				ResetAndPalyMusic ();
			}

			HandleIntent (intent);

			return  base.OnStartCommand (intent, flags, startId);
		}

		public override bool OnUnbind (Intent intent)
		{
			m_objMediaSession.Release ();
			return base.OnUnbind (intent);
		}

		public  void ResetAndPalyMusic ()
		{
			try {
				if (!(m_objMediaPlayer == null)) {
					m_objMediaPlayer.Reset ();
					m_objMediaPlayer.SetAudioStreamType (Stream.Music);

					m_objMediaPlayer.SetDataSource (mContext, Android.Net.Uri.Parse (musicUrl));  
					m_objMediaPlayer.Prepare ();
					m_objMediaPlayer.Start ();
				}
			} catch (Exception ex) {
				Log.Debug ("NMLMediaService", "Error:" + ex.ToString ());
		 
			}
		}

		public void InitMediaSessions ()
		{
			mContext = ApplicationContext;
			m_objMediaPlayer = new MediaPlayer ();
			m_objMediaSessionManager = (MediaSessionManager)GetSystemService (Context.MediaSessionService);
			m_objMediaSession = new MediaSession (this, "sample session");
			m_objMediaController = new Android.Media.Session.MediaController (this, m_objMediaSession.SessionToken);
			 
			m_objMediaSession.SetFlags (MediaSessionFlags.HandlesMediaButtons | MediaSessionFlags.HandlesTransportControls);
			m_objMediaSession.Active = true;

			PlaybackState state = new PlaybackState.Builder ()
				  .SetActions (PlaybackState.ActionPlay | PlaybackState.ActionPause |
			                      PlaybackState.ActionPlayFromMediaId | PlaybackState.ActionFastForward |
			                      PlaybackState.ActionSkipToNext | PlaybackState.ActionSkipToPrevious)
				.Build ();

			m_objMediaSession.SetPlaybackState (state);		 
			m_objMediaSession.SetCallback (new MediaEvent ());
		}
	}

}

